import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  //相当于实体类中的私有属性
  state: {
    //初始时从localStorage中获取token
    token: localStorage.getItem("token"),
    //初始时从localStorage中获取userInfo
    userInfo: JSON.parse(localStorage.getItem("userInfo"))
  },

  //相当于是实体类的setter方法，可以给上面的state中的属性赋值
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
      //存放到浏览器的本地storage中
      localStorage.setItem("token", token);
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo;
      //存放到浏览器的本地storage中
      localStorage.setItem("userInfo", JSON.stringify(userInfo));
    },
    REMOVE_INFO: state => {
      //将所有值赋值为空
      state.token  = '';
      state.userInfo  = {};
      localStorage.setItem("token", '');
      localStorage.setItem("userInfo", '');
    }
  },

  //相当于是实体类的getter方法，可以获取上面的state中的属性
  getters: {

    //获取token方法
    getToken: (state) => {
      return state.token;
    },

    //获取用户信息方法
    getUser: state =>{
      return state.userInfo;
    }
  },
  actions: {
  },
  modules: {
  }

})
